<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta property="og:title" content="Node.js封装自己的请求库" />
<meta property="og:description" content="人家Node.js明明有http库，不需要使用什么axios！主要是为了轻量级！！！
HTTPS的话，后续更新！
引入 1 const http = require(&#39;http&#39;) GET请求 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 function request(options) { return new Promise(resolve =&gt; { let contents,req,reqData; Toptions = options // 发送请求 req = http.request(Toptions, res =&gt; { res." />
<meta property="og:type" content="article" />
<meta property="og:url" content="http://wuju-i.gitee.io/post/20210702/" /><meta property="article:section" content="post" />
<meta property="article:published_time" content="2021-07-02T00:00:00+00:00" />
<meta property="article:modified_time" content="2021-07-02T00:00:00+00:00" />

  <meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="Node.js封装自己的请求库"/>
<meta name="twitter:description" content="人家Node.js明明有http库，不需要使用什么axios！主要是为了轻量级！！！
HTTPS的话，后续更新！
引入 1 const http = require(&#39;http&#39;) GET请求 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 function request(options) { return new Promise(resolve =&gt; { let contents,req,reqData; Toptions = options // 发送请求 req = http.request(Toptions, res =&gt; { res."/>

  
  <meta name="theme-color" media="(prefers-color-scheme: light)" content="#ffffff">
  <meta name="theme-color" media="(prefers-color-scheme: dark)" content="#262d33">
  <title>
    
    橘子的博客 - Node.js封装自己的请求库
    
  </title>
  
  <link rel="shortcut icon" href="g.png" type="image/x-icon" />
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&display=swap"
    rel="stylesheet">
  
  
  
  <link rel="stylesheet" href="/minima.1714031344.css">

  
  
  <script defer type="text/javascript" src="/minima.1714031344.js"></script>
</head>
<script>
  
  let theme_2b_used = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';

  try {
    if (!('theme' in localStorage)) {
      const default_theme = '';
      
      
      if (default_theme === 'dark' || default_theme === 'light') {
        theme_2b_used = default_theme;
      }

      
      localStorage.theme = theme_2b_used;
    }
    
    document.querySelector('html').classList.add(localStorage.theme);
  } catch (e) {
    console.error(e);
  }
</script>


<body class=" sm:mx-5 sm:my-0">
  <header class="flex justify-between items-center mb-6 sm:my-3">
  <div class="flex items-center">
    
    <div id="theme-switcher" class="text-4xl cursor-pointer">🌑</div>
  </div>
  <nav class="flex items-center
    whitespace-nowrap overflow-x-auto overflow-y-hidden">
    
    <a class="ml-5" href="/">首页</a>
    
    <a class="ml-5" href="/categories">分类</a>
    
    <a class="ml-5" href="/log">日记</a>
    
    <a class="ml-5" href="/about">关于</a>
    
  </nav>
</header>
  
  

<h1 class="mt-6 mb-6">Node.js封装自己的请求库</h1>
<div class="mb-3 text-xs flex justify-between sm:flex-col">
	<div>
	
		Posted at &mdash; 21年 7月2日
	
		
	</div>
	
</div>
<main>
	<p></p>
	<article class="md">
		<p>人家Node.js明明有http库，不需要使用什么axios！主要是为了轻量级！！！<br>
HTTPS的话，后续更新！</p>
<h2 id="引入">引入</h2>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-javascript" data-lang="javascript"><span class="line"><span class="cl"><span class="kr">const</span> <span class="nx">http</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;http&#39;</span><span class="p">)</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h2 id="get请求">GET请求</h2>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span><span class="lnt">41
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-javascript" data-lang="javascript"><span class="line"><span class="cl"><span class="kd">function</span> <span class="nx">request</span><span class="p">(</span><span class="nx">options</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">	<span class="k">return</span> <span class="k">new</span> <span class="nb">Promise</span><span class="p">(</span><span class="nx">resolve</span> <span class="p">=&gt;</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">		<span class="kd">let</span> <span class="nx">contents</span><span class="p">,</span><span class="nx">req</span><span class="p">,</span><span class="nx">reqData</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">		<span class="nx">Toptions</span> <span class="o">=</span> <span class="nx">options</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">		<span class="c1">// 发送请求
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="nx">req</span> <span class="o">=</span> <span class="nx">http</span><span class="p">.</span><span class="nx">request</span><span class="p">(</span><span class="nx">Toptions</span><span class="p">,</span> <span class="nx">res</span> <span class="p">=&gt;</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">			<span class="nx">res</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s1">&#39;data&#39;</span><span class="p">,</span><span class="nx">data</span> <span class="p">=&gt;</span> <span class="p">{</span> <span class="nx">reqData</span><span class="o">+=</span><span class="nx">data</span> <span class="p">});</span>
</span></span><span class="line"><span class="cl">		<span class="p">});</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// 超时挂断
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="nx">req</span><span class="p">.</span><span class="nx">setTimeout</span><span class="p">(</span><span class="mi">5000</span><span class="p">,</span><span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">			<span class="nx">req</span><span class="p">.</span><span class="nx">destroy</span><span class="p">();</span>
</span></span><span class="line"><span class="cl">		<span class="p">});</span>
</span></span><span class="line"><span class="cl">		<span class="nx">req</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s1">&#39;end&#39;</span><span class="p">,</span> <span class="p">()</span> <span class="p">=&gt;</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">			<span class="nx">resolve</span><span class="p">(</span><span class="nx">reqData</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">		<span class="p">});</span>
</span></span><span class="line"><span class="cl">		<span class="nx">req</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s1">&#39;error&#39;</span><span class="p">,</span> <span class="p">(</span><span class="nx">e</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">			<span class="nx">resolve</span><span class="p">({</span>
</span></span><span class="line"><span class="cl">				<span class="nx">code</span> <span class="o">:</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">				<span class="nx">message</span> <span class="o">:</span> <span class="nx">e</span><span class="p">.</span><span class="nx">message</span>
</span></span><span class="line"><span class="cl">			<span class="p">});</span>
</span></span><span class="line"><span class="cl">		<span class="p">});</span>
</span></span><span class="line"><span class="cl">		<span class="c1">//必须调用end()
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="nx">req</span><span class="p">.</span><span class="nx">end</span><span class="p">();</span>
</span></span><span class="line"><span class="cl">	<span class="p">});</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="c1">// 使用
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="nx">request</span><span class="p">({</span>
</span></span><span class="line"><span class="cl">	<span class="nx">host</span> <span class="o">:</span> <span class="s1">&#39;localhost&#39;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">	<span class="nx">port</span> <span class="o">:</span> <span class="mi">80</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">	<span class="nx">path</span> <span class="o">:</span> <span class="s1">&#39;/&#39;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">	<span class="c1">// 请求类型
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>	<span class="nx">method</span><span class="o">:</span><span class="s1">&#39;GET&#39;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">	<span class="c1">// 头部
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>	<span class="nx">headers</span> <span class="o">:</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">		<span class="s1">&#39;Content-Type&#39;</span> <span class="o">:</span> <span class="s1">&#39;application/json&#39;</span>
</span></span><span class="line"><span class="cl">	<span class="p">},</span>
</span></span><span class="line"><span class="cl"><span class="p">}).</span><span class="nx">then</span><span class="p">(</span><span class="nx">res</span> <span class="p">=&gt;</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">	<span class="c1">// res是Buffer 可以.toString()获取字符串类型
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>	<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">res</span><span class="p">.</span><span class="nx">toString</span><span class="p">())</span>
</span></span><span class="line"><span class="cl"><span class="p">})</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h2 id="post-请求">POST 请求</h2>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span><span class="lnt">41
</span><span class="lnt">42
</span><span class="lnt">43
</span><span class="lnt">44
</span><span class="lnt">45
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-javascript" data-lang="javascript"><span class="line"><span class="cl"><span class="kd">function</span> <span class="nx">request</span><span class="p">(</span><span class="nx">options</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">	<span class="k">return</span> <span class="k">new</span> <span class="nb">Promise</span><span class="p">(</span><span class="nx">resolve</span> <span class="p">=&gt;</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">		<span class="kd">let</span> <span class="nx">contents</span><span class="p">,</span><span class="nx">req</span><span class="p">,</span><span class="nx">reqData</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">		<span class="nx">Toptions</span> <span class="o">=</span> <span class="nx">options</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// 发送请求
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="nx">req</span> <span class="o">=</span> <span class="nx">http</span><span class="p">.</span><span class="nx">request</span><span class="p">(</span><span class="nx">Toptions</span><span class="p">,</span> <span class="nx">res</span> <span class="p">=&gt;</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">			<span class="nx">res</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s1">&#39;data&#39;</span><span class="p">,</span><span class="nx">data</span> <span class="p">=&gt;</span> <span class="p">{</span> <span class="nx">reqData</span><span class="o">+=</span><span class="nx">data</span><span class="p">;</span> <span class="p">});</span>
</span></span><span class="line"><span class="cl">		<span class="p">});</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// 发送数据
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="nx">req</span><span class="p">.</span><span class="nx">write</span><span class="p">(</span><span class="nx">contents</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">		<span class="c1">// 超时挂断
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="nx">req</span><span class="p">.</span><span class="nx">setTimeout</span><span class="p">(</span><span class="mi">5000</span><span class="p">,</span><span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">			<span class="nx">req</span><span class="p">.</span><span class="nx">destroy</span><span class="p">();</span>
</span></span><span class="line"><span class="cl">		<span class="p">});</span>
</span></span><span class="line"><span class="cl">		<span class="nx">req</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s1">&#39;end&#39;</span><span class="p">,</span> <span class="p">()</span> <span class="p">=&gt;</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">			<span class="nx">resolve</span><span class="p">(</span><span class="nx">reqData</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">		<span class="p">});</span>
</span></span><span class="line"><span class="cl">		<span class="nx">req</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s1">&#39;error&#39;</span><span class="p">,</span> <span class="p">(</span><span class="nx">e</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">			<span class="nx">resolve</span><span class="p">({</span>
</span></span><span class="line"><span class="cl">				<span class="nx">code</span> <span class="o">:</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">				<span class="nx">message</span> <span class="o">:</span> <span class="nx">e</span><span class="p">.</span><span class="nx">message</span>
</span></span><span class="line"><span class="cl">			<span class="p">});</span>
</span></span><span class="line"><span class="cl">		<span class="p">});</span>
</span></span><span class="line"><span class="cl">		<span class="c1">//必须调用end()
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>		<span class="nx">req</span><span class="p">.</span><span class="nx">end</span><span class="p">();</span>
</span></span><span class="line"><span class="cl">	<span class="p">});</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="c1">// 使用
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="kd">let</span> <span class="nx">data</span> <span class="o">=</span> <span class="s1">&#39;user=123&#39;</span> <span class="c1">// 你可以发送json数据 JSON.stringify
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="nx">request</span><span class="p">({</span>
</span></span><span class="line"><span class="cl">	<span class="nx">host</span> <span class="o">:</span> <span class="s1">&#39;localhost&#39;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">	<span class="nx">port</span> <span class="o">:</span> <span class="mi">80</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">	<span class="nx">path</span> <span class="o">:</span> <span class="s1">&#39;/login&#39;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">	<span class="c1">// 请求类型
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>	<span class="nx">method</span><span class="o">:</span><span class="s1">&#39;POST&#39;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">	<span class="nx">data</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">	<span class="c1">// 头部
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>	<span class="nx">headers</span> <span class="o">:</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">		<span class="s1">&#39;Content-Type&#39;</span> <span class="o">:</span> <span class="s1">&#39;application/json&#39;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">		<span class="s1">&#39;Content-Length&#39;</span> <span class="o">:</span> <span class="nx">data</span><span class="p">.</span><span class="nx">length</span>
</span></span><span class="line"><span class="cl">	<span class="p">},</span>
</span></span><span class="line"><span class="cl"><span class="p">}).</span><span class="nx">then</span><span class="p">(</span><span class="nx">res</span> <span class="p">=&gt;</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">	<span class="c1">// res是Buffer 可以.toString()获取字符串类型
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>	<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">res</span><span class="p">.</span><span class="nx">toString</span><span class="p">())</span>
</span></span><span class="line"><span class="cl"><span class="p">})</span>
</span></span></code></pre></td></tr></table>
</div>
</div>
	</article>
</main>





	

	

	



  <footer class="mt-8 flex sm:flex-col-reverse justify-between items-center">
  <p class="mt-0 text-sm">
    © 2022 | 橘子
    
  </p>
  <p class="flex items-center mt-0">
    
  </p>
</footer>
</body>
</html>